import api from "@/api/apiConfig";
import ajax from "@/api/http";
import store from "@/store";
import router from "@/router";
import { message } from "ant-design-vue";

export default {
  goPage(routePath,query) {
    router.push({
      path: routePath,
      query: query
    });
  },
  getInfo(cb) {
    if (localStorage.getItem("mtoken")) {
      ajax
        .post(api.getPerson)
        .then(res => {
          store.dispatch("changeMobile", res.data.mobile);
          store.dispatch("changeLoginStatus", true);
          store.dispatch("changeValidate", res.data.is_check);
          store.dispatch("changeMobileStatus", res.data.safeMobile);
          store.dispatch("changeSetPay", res.data.pay_password);
          store.dispatch("changeSign", res.data.is_sign);
          store.dispatch("changeAccount", res.data.username);
          store.dispatch("changeMemberType", res.data.member_type);
          cb && cb();
        })
        .catch(err => {
          message.error(err);
          cb && cb();
        });
    } else {
      store.dispatch("resetState");
      cb && cb();
    }
  },
  logout(routerName) {
    ajax
      .post(api.logout)
      .then(() => {
        localStorage.removeItem("mtoken");
        sessionStorage.clear();
        router.push({ name: routerName });
        store.dispatch("changeLoginStatus", false);
      })
      .catch(err => {
        message.error(err);
      });
  },
};
